package com.ydd.controller.amidn;

import com.ydd.dto.ArticlePageDTO;
import com.ydd.dto.YddArticleDTO;
import com.ydd.entity.YddArticle;
import com.ydd.result.PageResult;
import com.ydd.result.Result;
import com.ydd.service.ArticleService;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;



@RestController
@RequestMapping("/ydd_article")
@Slf4j
public class ArticleController {


    @Autowired
    ArticleService articleService;
    /**
     * 新增文章
     */
    @PostMapping("/add")
    public Result save(@RequestBody YddArticleDTO yddArticleDTO){
        log.info("新增文章：{}", yddArticleDTO);
        articleService.saveWithFlavor(yddArticleDTO);
        return Result.success();
    }

    /**
     * 分页查询文章列表
     * @param dto
     * @return
     */
    @GetMapping("/list")
    public Result<PageResult> page(ArticlePageDTO articlePageDTO){
        PageResult pageResult =  articleService.articlePageQuery(articlePageDTO);
        log.info("分页查询：{}",articlePageDTO);
        log.info("总记录数：{}",pageResult.getTotal());
        return Result.success(pageResult);
    }

    @GetMapping("/rowInfo")
    @ApiOperation("回显id")
    public Result rowInfo(String id){
        log.info("回显id：{}",id);
        YddArticle yddArticle = articleService.rowInfo(id);
        return Result.success(yddArticle);
    }

    /**
     * 删除文章
     *
     */
    @GetMapping("/del")
    @ApiOperation("删除文章")
    public Result deleteId(String id){
        log.info("根据id删除数据：{}",id);
        articleService.deleteId(id);
        return Result.success();
    }

    /**
     * 修改文章
     */

    @PostMapping("/update")
    public Result<String> update(@RequestBody YddArticleDTO yddArticleDTO){

        log.info("修改文章:{}",yddArticleDTO);

        return articleService.update(yddArticleDTO);

    }


}